package main

import (
	"fmt"
	"os"
	"runtime"

	"gddgame.cc/galaxy"
	"gddgame.cc/galaxy/binocle/galaxy_provide"
	"gddgame.cc/galaxy/common/bootstrap"
	"gddgame.cc/galaxy/project/breeder/breeder"
	"gddgame.cc/galaxy/project/manager/manager"
	"gddgame.cc/galaxy/project/maze/maze"
	"gddgame.cc/galaxy/project/orbit/orbit"
	"gddgame.cc/galaxy/project/star/star"
)

func main() {
	galaxy.SetInfo("打包函数", "", star.Version)
	if err := galaxy.Init(func() error {
		cpu := runtime.NumCPU()
		procs := runtime.GOMAXPROCS(0)
		fmt.Println("cpu num:", cpu, " GOMAXPROCS:", procs)
		return galaxy.PreStart(bootstrap.DefaultBoot.ParseConfig)
	}, func() error {
		return galaxy.Start(
			registerBinocle,
			registerBinocleAgent,
			registerManager,
			registerOrbit,
			//registerStar,
			//registerMaze,
			registerBreeder,
		)
	}); err != nil {
		fmt.Fprint(os.Stderr, err)
		fmt.Print("\n")
		os.Exit(1)
	}
}

func registerBinocle() error {
	return galaxy.RegisterServer(galaxy_provide.CoreProvide())
}

func registerBinocleAgent() error {
	return galaxy.RegisterServer(galaxy_provide.AgentProvide())
}
func registerManager() error {
	component := &manager.Component{Config: &manager.Config{}}

	return galaxy.RegisterServer(galaxy.ServerProvider{
		Name:   "manager",
		Config: component.Config,
		Inject: component,
		OnCreate: func() error {
			component.Component(component)
			return nil
		},
	})
}

func registerStar() error {
	component := &star.Component{Config: &star.Config{}}

	return galaxy.RegisterServer(galaxy.ServerProvider{
		Name:   "star",
		Config: component.Config,
		Inject: component,
		OnCreate: func() error {
			component.Component(component)
			return nil
		},
		OnStop: func() error {
			return component.Nebula.Close()
		},
	})
}

func registerMaze() error {
	component := &maze.Component{Config: &maze.Config{}}

	return galaxy.RegisterServer(galaxy.ServerProvider{
		Name:   "maze",
		Config: component.Config,
		Inject: component,
		OnCreate: func() error {
			component.Component(component)
			return nil
		},
		OnStop: func() error {
			return component.Nebula.Close()
		},
	})
}

func registerBreeder() error {
	component := &breeder.Component{Config: &breeder.Config{}}

	return galaxy.RegisterServer(galaxy.ServerProvider{
		Name:   "breeder",
		Config: component.Config,
		Inject: component,
		OnCreate: func() error {
			component.Component(component)
			return nil
		},
		OnStop: func() error {
			return component.Nebula.Close()
		},
	})
}

func registerOrbit() error {
	component := &orbit.Component{Config: &orbit.Config{}}

	return galaxy.RegisterServer(galaxy.ServerProvider{
		Name:   "orbit",
		Config: component.Config,
		Inject: component,
		OnCreate: func() error {
			component.Component(component)
			return nil
		},
		OnStop: func() error {
			return component.Comet.Close()
		},
	})
}
